package com.wd.jetpack.ui.viewmodel

import androidx.databinding.ObservableArrayList
import androidx.lifecycle.ViewModel
import com.wd.jetpack.api.ArticleService
import com.wd.jetpack.api.network.BaseNetProvider
import com.wd.jetpack.api.network.NetMgr
import com.wd.jetpack.helper.exytens.async
import com.wd.jetpack.model.ArticleData
import com.wd.jetpack.model.BaseResponse
import io.reactivex.Single

class ListViewModel : ViewModel() {
    private val serviceUrl = "http://v.juhe.cn"
    private val type = "top"
    private val key = "11c615153228e4410561972008d73cd2"

    //声明http
    private var remote =
            NetMgr.getRetrofit(serviceUrl, BaseNetProvider()).create(ArticleService::class.java)

    //执行http访问
    fun getArticle(): Single<BaseResponse<ArticleData>> = remote.toutiao(type, key).async().doOnSuccess {
        setData(it.data)
    }

    //声明列表数据
    var items = ObservableArrayList<ListItemViewModel>()

    //数据装载
    private fun setData(data: ArticleData) {
        items.clear()
        data.data?.let {
            data.data.map {
                items.add(ListItemViewModel().set(it))
            }
        }
    }
}